package org.jeecg.modules.lab.utensil.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.lab.chemical.entity.LabChemical;
import org.jeecg.modules.lab.utensil.entity.*;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilChecklistMapper;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilMapper;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilNoteMapper;
import org.jeecg.modules.lab.utensil.service.ILabUtensilService;
import org.jeecg.modules.lab.utensil.vo.LabUtensilCheckVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 器具信息
 * @Author: jeecg-boot
 * @Date: 2023-07-11
 * @Version: V1.0
 */
@Service
@Slf4j
public class LabUtensilServiceImpl extends ServiceImpl<LabUtensilMapper, LabUtensil> implements ILabUtensilService {

    @Autowired
    private LabUtensilMapper labUtensilMapper;

    @Autowired
    private LabUtensilChecklistMapper labUtensilChecklistMapper;
    @Autowired
    private LabUtensilNoteMapper utensilNoteMapper;

    @Override
    public void removeByDeviceId(String devId) {
        labUtensilMapper.deleteByDeviceId(devId);
    }


    @Override
    public List<LabUtensilTo> queryType() {
        return labUtensilMapper.queryType();
    }

    @Override
    public List<LabUtensilTo> queryStatus() {
        return labUtensilMapper.queryStatus();
    }

    /**
     * 统计数量
     *
     * @return
     */
    @Override
    public List<LabUtensilTo> countAll() {
        List<LabUtensilTo> list = new ArrayList<>();
        //器具总数
        String countAll = labUtensilMapper.countAll();
        list.add(new LabUtensilTo("器具总数",String.valueOf(countAll),"green"));
        //历史检测总数
        Long aLong = labUtensilChecklistMapper.selectCount(new LambdaQueryWrapper<LabUtensilChecklist>().isNotNull(LabUtensilChecklist::getId));
        list.add(new LabUtensilTo("历史检测数",String.valueOf(aLong),"orange"));
        //检测数
        Long countTest = labUtensilMapper.selectCount(new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getStatus, "检定中"));
        list.add(new LabUtensilTo("检测中",String.valueOf(countTest),"blue"));
        //报废数
        Long countStop = labUtensilMapper.selectCount(new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getStatus, "停用"));
        list.add(new LabUtensilTo("报废数",String.valueOf(countStop),"red"));
        return list;
    }

    @Override
    public void updateSure(LabUtensil labUtensil) {
            if ("停用".equals(labUtensil.getConfirmResult())){
                labUtensil.setStatus("异常");
            }else {
                labUtensil.setStatus("正常");
            }
            labUtensil.setInspDecide("已确认");
            labUtensil.setSynFlag("1");
            labUtensilMapper.updateById(labUtensil);
            LabUtensilNote labUtensilNote = new LabUtensilNote();
            BeanUtils.copyProperties(labUtensil,labUtensilNote);
            labUtensilNote.setUteId(labUtensil.getId());
            labUtensilNote.setId("");
            utensilNoteMapper.insert(labUtensilNote);
    }

    @Override
    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(fixedDelay = 3000)
    public void updateStatus() {
        log.info("每日2点判断器具状态");
        List<LabUtensil> labUtensils = labUtensilMapper.selectList(new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getStatus, "正常"));
        labUtensils.forEach(e->{
            if (e.getPlanInspectDate()!=null) {
                if (e.getPlanInspectDate().before(new Date())) {
                    e.setStatus("待检定");
                    labUtensilMapper.updateById(e);
                }
            }
        });
    }


}
